package com.cj.controller;

import com.cj.po.Class;
import com.cj.po.PageInfo;
import com.cj.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ClassController {
    @Autowired
    private ClassService classService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findClass")
    public String findClass(String c_classid, String c_classname, String c_counsellor,
                            Integer pageIndex, Integer pageSize, Model model) {
        PageInfo<Class> pi = classService.findPageInfo(c_classid, c_classname, c_counsellor, pageIndex, pageSize);
        model.addAttribute("pi", pi);
        model.addAttribute("c_classid", c_classid);
        model.addAttribute("c_classname", c_classname);
        model.addAttribute("c_counsellor", c_counsellor);
        return "class_list";
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportClassList", method = RequestMethod.POST)
    @ResponseBody
    public List<Class> exportClass() {
        List<Class> classList = classService.getAllClasses();
        return classList;
    }

    /**
     * 删除班级信息
     */
    @RequestMapping("/deleteClass")
    @ResponseBody
    public String deleteClass(Integer c_id) {
        int s = classService.deleteClass(c_id);
        return "class_list";
    }

    /**
     * 添加班级信息
     */
    @RequestMapping(value = "/addClass", method = RequestMethod.POST)
    @ResponseBody
    public String addClass(@RequestBody Class clazz) {
        int s = classService.addClass(clazz);
        return "class_list";
    }

    /**
     * 修改班级信息
     */
    @RequestMapping(value = "/updateClass", method = RequestMethod.POST)
    public String updateClass(Class clazz) {
        int s = classService.updateClass(clazz);
        return "redirect:/findClass";
    }

    @RequestMapping("/findClassById")
    public String findClassById(Integer c_id, HttpServletRequest request) {
        Class clazz = classService.findClassById(c_id);
        request.setAttribute("clazz", clazz);
        return "class_edit";
    }
}
